package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.ui.adapter.ViewPagerInsideBindingAdapter;
import com.whmnx.my.ui.fragment.InsideTradingFragment;
import com.whmnx.my.ui.fragment.JiaoYiFragment;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/10/10 0010 14:23
 * version: 1.0
 * @author Administrator
 */
public class InsideModel extends ToolbarViewModel{
    public InsideTradingFragment fragment;

    //给ViewPager添加ObservableList
    public ObservableList<ViewPagerItemInsideViewModel> items = new ObservableArrayList<>();


    //给ViewPager添加ItemBinding
    public ItemBinding<ViewPagerItemInsideViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_inside_viewpager);


    private String[] titles = new String[]{"播豆收购","播豆出售"};

    public InsideModel(@NonNull Application application) {
        super(application);
    }


    public void initToolbar(InsideTradingFragment insideTradingFragment) {
        this.fragment = insideTradingFragment;
        setTitleText("内盘交易所");
        setRightText("交易记录");
        setRightTextVisible(View.VISIBLE);
        getUserInfo();
    }

    private void getUserInfo() {
        Map<String, Object> params = new HashMap<>();
        params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
        HttpClient.get(fragment.getActivity(), Constants.GETUSERINFO, params,new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {
                SPUtils.getInstance().put(Constants.BEAN, result.Bean);
                SPUtils.getInstance().put(Constants.AVERAGEPRICE, result.AveragePrice);
                SPUtils.getInstance().put(Constants.POUNDAGE, result.Poundage);

            }
        });
    }

    public void addPage() {
        items.clear();
        //模拟3个ViewPager页面
        for (int i = 1; i <= titles.length; i++) {
            ViewPagerItemInsideViewModel itemViewModel = new ViewPagerItemInsideViewModel(this,  i);
            items.add(itemViewModel);
        }
    }


    //给ViewPager添加PageTitle
    public final BindingViewPagerAdapter.PageTitles<ViewPagerItemInsideViewModel> pageTitles = new BindingViewPagerAdapter.PageTitles<ViewPagerItemInsideViewModel>() {
        @Override
        public CharSequence getPageTitle(int position, ViewPagerItemInsideViewModel item) {
            return titles[position];
        }
    };
    //给ViewPager添加Adpter，请使用自定义的Adapter继承BindingViewPagerAdapter，重写onBindBinding方法
    public final ViewPagerInsideBindingAdapter adapter = new ViewPagerInsideBindingAdapter();
    //ViewPager切换监听
    public BindingCommand<Integer> onPageSelectedCommand = new BindingCommand<>(new BindingConsumer<Integer>() {
        @Override
        public void call(Integer index) {

        }
    });

    @Override
    protected void  rightTextOnClick(){
        startContainerActivity(JiaoYiFragment.class.getCanonicalName());

    }
}
